<?php

class Product {

    public $node;
    public $nid;
    public $title;
    public $category;
    public $rate;
    public $brand;
    public $id;
    public $listed_price;
    public $sell_price;
    public $saving;
    public $color;
    public $size;
    public $usage;
    public $material;
    public $motifs;
    public $thumbnails;    
    public $description;    
    public $size_info;

    public function __construct($nid) {
        $this->nid = $nid;
        $node = node_load($nid);
        if($node->type!='product')
            return false;
        $this->node=$node;
        $this->title = $node->title;
        $this->category = array(
            'tid' => $node->field_product_category[LANGUAGE_NONE][0]['tid'],
            'name' => get_term_name_by_tid($node->field_product_category[LANGUAGE_NONE][0]['tid'])
        );
//        var_dump($node->field_rate);
//        die;
//        if(isset($node->field_rate[LANGUAGE_NONE])){
//            $this->rate = array(
//                'user'=> $node->field_rate[LANGUAGE_NONE][0]['user'],
//                'average'=> $node->field_rate[LANGUAGE_NONE][0]['average'],
//                'count'=> $node->field_rate[LANGUAGE_NONE][0]['count'],
//            );
//        }else{
//            $this->rate = array();
//        }
        
        if(isset($node->field_product_brand[LANGUAGE_NONE])){        
            $this->brand = array(
                'tid' => $node->field_product_brand[LANGUAGE_NONE][0]['tid'],
                'name' => get_term_name_by_tid($node->field_product_brand[LANGUAGE_NONE][0]['tid'])
            );
        }else{
            $this->brand=array();
        }
        
        $this->id = isset($node->field_product_id[LANGUAGE_NONE])? $node->field_product_id[LANGUAGE_NONE][0]['value']:'';
        $this->listed_price = isset($node->field_listed_price[LANGUAGE_NONE]) ? $node->field_listed_price[LANGUAGE_NONE][0]['value']: '';
        $this->sell_price = isset($node->field_sell_price[LANGUAGE_NONE])? $node->field_sell_price[LANGUAGE_NONE][0]['value']:'';
        $this->saving = isset($node->field_saving[LANGUAGE_NONE])?$node->field_saving[LANGUAGE_NONE][0]['value']:'';
        
        $this->color = array();
        foreach ($node->field_color[LANGUAGE_NONE] as $color) {
            $this->color[] = $color['rgb'];
        }
        $this->size = array();
        foreach ($node->field_product_size[LANGUAGE_NONE] as $temp) {
            $this->size[$temp['tid']]  = get_term_name_by_tid($temp['tid']);
        }
        $this->usage = array();
        if(isset($node->field_product_usage[LANGUAGE_NONE])){
            foreach ($node->field_product_usage[LANGUAGE_NONE] as $temp) {
                $this->usage[$temp['tid']] =  get_term_name_by_tid($temp['tid']);
            }
        }
        
        $this->material = array();
        if(isset($node->field_product_material[LANGUAGE_NONE])){
            $this->material = array(
                $node->field_product_material[LANGUAGE_NONE][0]['tid']
                => get_term_name_by_tid($node->field_product_material[LANGUAGE_NONE][0]['tid'])
            );
        }
        
        $this->motifs = array();
        if(isset($node->field_product_motifs[LANGUAGE_NONE])){
            $this->motifs[$node->field_product_motifs[LANGUAGE_NONE][0]['tid']]
                = get_term_name_by_tid($node->field_product_motifs[LANGUAGE_NONE][0]['tid']);
        }
        $this->thumbnails = array();
        foreach ($node->field_product_thumbnail[LANGUAGE_NONE] as $temp) {
            $this->thumbnails[] = $temp['uri'];
        }
        
        if(isset($node->field_size_info[LANGUAGE_NONE])){
            $this->size_info = $node->field_size_info[LANGUAGE_NONE][0]['uri'];        
        }
        $this->description = isset($node->field_product_description[LANGUAGE_NONE])? $node->field_product_description[LANGUAGE_NONE][0]['value']:'';
    }

    public function display_field_rate_widget() {
        $settings = array(
            'content_type' => 'node',
            'content_id' => $this->nid,
            'entity' => $this->node,
            'stars' => 5,
            'field_name' => 'field_rate_rating', // field_x_rating
            'autosubmit' => TRUE,   
            'allow_clear' => TRUE,
            'langcode' => 'und',
            'description' => '',
            'text' => 'none', // options are none, average, smart, user, and dual
            'tag' => 'vote',
            'style' => 'average', // options are average, dual, smart, user
            'widget' => array('name' => 'oxygen', 'css' => drupal_get_path('module', 'fivestar') . '/widgets/oxygen/oxygen.css')
        );
        $form = drupal_get_form('fivestar_custom_widget', $this->node->field_rate[LANGUAGE_NONE][0], $settings);
        return render($form);        
        
    }

    public function display_product_info($is_popup = false) {           
        
        // carousel thumbnail
        $s = '<div id="product-thumbnails" class="col">';
            $s.='<ul id="product-detail-carousel" class="jcarousel-skin-tango">';
            $i=0;
            foreach ($this->thumbnails as $tmp_path) {
                $s.= '<li><img src="' . image_style_url('product_detail_carousel', $tmp_path) . '" index="'.$i.'" /></li>';
                $i++;
            }
            $s.='</ul>';
        $s.='</div>';

        // zoom affect
        $s.='<div id="zoom-controls" class="col">';
        $i=0;
        foreach ($this->thumbnails as $tmp_path) {
            $s.= '<a href="'.image_style_url('product_detail_large_thumbnail', $tmp_path).'" class="zoom-wrapper" index="'.$i.'">';
                $s.='<img class="selected-img" src="'.image_style_url('product_detail_main_thumbnail', $tmp_path).'" />';
            $s.='</a>';                
            $i++;
        }        
        $s.='</div>';
        
        // tabs product info
        $s.= '<div id="detail-info" class="col">';
            $s .='<div id="tabs-title">';
                $s.='<span class="tab-title active" tab="1">Thông tin sản phẩm</span>';
                $s.='<span class="tab-title" tab="2">Quy định đổi trả hàng</span>';
            $s.='</div>';
            $s .='<div id="tabs-info">';
                $s.='<div class="tab-info" tab="1">';
                    $s.='<h1 id="page-title">'.$this->title.'</h1>';                    
                    if($is_popup==false)
                        $s.=$this->display_field_rate_widget();
                    $s.=' <a id="go-comment" href="#comment-area">Viết đánh giá</a>';   
                    $s.='<div class="clearfix"></div>';
                
                    $s.='<div>';
                        $s.='<div class="col col-left">';
                            $s.='<div class="field brand"><b>Thương hiệu</b>: <span>'.$this->brand['name'].'</span></div>';
                            $s.='<div class="field code"><b>Mã hàng</b>: <span>'.$this->id.'<span></div>';
                            if($this->listed_price && $this->listed_price!=0){
                                $s.='<div class="field listed-price"><b>Giá niêm yết</b>: <span class="value listed-price">'.display_price($this->listed_price).'</span></div>';
                            }
                            $s.='<div class="field sell-price"><b>Giá bán</b>: <span>'.display_price($this->sell_price).'</span></div>';
                            if($this->saving!=0){
                                $s.='<div class="field saving"><b>Tiết kiệm</b>: <span>'.$this->saving.'%</span></div>';
                            }
                            
                        $s.='</div>'; // End col left
                        
                        $s.='<div class="col col-right">';
                            $s.='<div class="field color"><b>Màu sắc</b><div>';
                            foreach($this->color as $color){
                                $s.='<span class="color" color-value="'.$color.'" style="background-color:'.$color.'"></span>';
                            }
                            $s.='</div></div>';
                            $s.='<div class="field size"><b>Kích cỡ</b><div>';
                            $arr_size=  array_values($this->size);
                            
                         //var_dump($arr_size); die;
                            foreach($this->size as $size){
                                $s.='<span class="size" size-value="'.$size.'">'.$size.'</span>';
                            }
                            $s.='</div></div>';
                            $s.='<div class="field amount"><b>Số lượng mua</b>: <input type="text" id="txtAmount" nid="'.$this->nid.'" /></div>';
                            $s.='<div class="field"><input type="button" class="btn" id="btnAddToCart" value="Mua hàng" /></div>';
                            
                        $s.='</div>'; // end col-right                                                
                    $s.='</div>';
                    
                   
                    
                $s.= '</div>';
                $s.='<div class="tab-info" tab="2">Nội dung tab Quy định đổi trả hàng</div>';
            $s.='</div>';
        $s.= '</div>';
        return $s;
    }
    
    public function display_product_detail_info() {        
        $s ='<h2 class="block-title">Thông tin chi tiết sản phẩm</h2>';
        $s.='<div class="content">';
        $s.='<div id="product-description">'.$this->description.'</div>';
      
        $header = array();
        $rows = array();
        $rows[] = array('Danh mục sản phẩm', $this->category['name']);
        $rows[] = array('Mục đích sử dụng', implode($this->usage,","));
        $rows[] = array('Màu sắc', $this->display_product_colors());
        $rows[] = array('Chất liệu', implode($this->material,","));
        $rows[] = array('Họa tiết', implode($this->motifs,","));

        $s.='<div class="infoTable">';
        $s.=theme('table', array('header'=>$header, 'rows'=>$rows));
        $s.='</div>';
        $s.='</div>';
        return $s;
    }
    
    public function display_product_colors(){
        $s='';
        foreach($this->color as $color) {
            $s.='<span class="color" style="background-color:' . $color . '"></span>';
        }
        return $s;
    }
    
    public function get_products_same_brand($brand_id = NULL) {
        if ($brand_id == NULL)
            $brand_id = $this->brand['tid'];

        $query = new EntityFieldQuery();
        $query->entityCondition('entity_type', 'node')
                ->entityCondition('bundle', 'product')
                ->propertyCondition('status', 1)
                ->propertyCondition('nid', $this->nid, '<>')
                ->fieldCondition('field_product_brand', 'tid', $brand_id, '=');                
        $result = $query->execute();
        
        $products = array();
        if(isset($result['node'])){
            foreach($result['node'] as $tmp){
                $p = new Product($tmp->nid);
                $products[] = $p;
            }        
        }
        return $products;
    }
    
    public function get_products_same_price($price=NULL) {
        if ($price == NULL)
            $price = $this->sell_price;

        $query = new EntityFieldQuery();
        $query->entityCondition('entity_type', 'node')
                ->entityCondition('bundle', 'product')
                ->propertyCondition('status', 1)
                ->propertyCondition('nid', $this->nid, '<>')
                ->fieldCondition('field_sell_price', 'value', array($price-100, $price+100), 'between');                
        $result = $query->execute();
        
        $products = array();
        if(isset($result['node'])){        
            foreach($result['node'] as $tmp){
                $p = new Product($tmp->nid);
                $products[] = $p;
            }
        }
        return $products;
    }
        
    
    public function display_related_products($products){        
        if(count($products)==0)
            return '';
        
        $count = count($products);        
        if($count % 2 == 1){
            $count--;
        }
        
        $s = '<ul class="related-products jcarousel-skin-tango">';
        for($i=0; $i<$count; $i+=2){            
            $s.='<li>';
                $s.='<div class="col">';
                    $s.='<a href="'.  url('node/'.$products[$i]->nid).'" title="'.$products[$i]->title.'"><img src="'.  image_style_url('product_related', $products[$i]->thumbnails[0]).'" /></a>';
                    $s.='<a href="'.  url('node/'.$products[$i]->nid).'" title="'.$products[$i]->title.'">'.$products[$i]->title.'</a>';
                    $s.='<div class="price"><span class="sell-price">'.display_price($products[$i]->sell_price).'</span><span class="listed-price">'.display_price($products[$i]->listed_price).'</span></div>';
                    if($products[$i]->saving!=0)
                        $s.='<div class="saving">'.$products[$i]->saving.'%</div>';
                $s.='</div>';
                $s.='<div class="col">';
                    $s.='<a href="'.  url('node/'.$products[$i+1]->nid).'" title="'.$products[$i+1]->title.'"><img src="'.  image_style_url('product_related', $products[$i+1]->thumbnails[0]).'" /></a>';
                    $s.='<a href="'.  url('node/'.$products[$i+1]->nid).'" title="'.$products[$i+1]->title.'">'.$products[$i+1]->title.'</a>';
                    $s.='<div class="price"><span class="sell-price">'.display_price($products[$i+1]->sell_price).'</span><span class="listed-price">'.display_price($products[$i+1]->listed_price).'</span></div>';
                    if($products[$i+1]->saving!=0)
                        $s.='<div class="saving">'.$products[$i+1]->saving.'%</div>';
                $s.='</div>';
            $s.='</li>';
        }
        if (count($products) % 2 == 1) {
            $s.='<li><div class="col">';
               $s.='<a href="'.  url('node/'.$products[$count]->nid).'" title="'.$products[$count]->title.'"><img src="'.  image_style_url('product_related', $products[$count]->thumbnails[0]).'" /></a>';
               $s.='<a href="'.  url('node/'.$products[$count]->nid).'" title="'.$products[$count]->title.'">'.$products[$count]->title.'</a>';
               $s.='<div class="price"><span class="sell-price">'.display_price($products[$count]->sell_price).'</span><span class="listed-price">'.display_price($products[$count]->listed_price).'</span></div>';
               if($products[$count]->saving!=0)
                   $s.='<div class="saving">'.$products[$count]->saving.'%</div>';
            $s.='</div></li>';
        }

        $s.='</ul>';
        return $s;
    }
    
    public function display_product_comments(){
        $s='';
        $cids = comment_get_thread($this->node, COMMENT_MODE_THREADED, 100);
        $comments = comment_load_multiple($cids);
        
        $s.='<div id="list-comments">';
        foreach($comments as $comment){        
            $s.=display_comment($comment);            
        }
        $s.='</div>';
        
//        $comment = new stdClass();
//        $comment->nid = $this->nid;
//        $form = drupal_get_form('comment_form', $comment);
//        $s.= drupal_render($form);
        
        //$comment = comment_load(1);
        //var_dump($comment);
        $s.='<h2 id="comment-area" class="block-title">Đánh giá</h2>';
        $s.='<div class="field vote"><span class="title">Chấm điểm cho sản phẩm</span>'.$this->display_field_rate_widget().'</div>';
        $s.='<div class="field title"><span class="title">Tiêu đề</span><span class="form-input"><input type="text" id="txtCommentTitle" /></span></div>';
        $s.='<div class="field content"><span class="title">Nội dung</span><span class="form-input"><textarea id="txtCommentBody"></textarea></span></div>';
        $s.='<div class="field submit"><input type="submit" class="btn" value="Đánh giá" id="btnSendComment" nid="'.$this->nid.'"/></div>';
        return $s;        
    }
    
}